package com.jd.dict.helper;

import com.jd.dict.config.ConfigClass;
import com.jd.dict.model.TableFieldInfoModel;
import com.jd.dict.model.TableIndexInfoModel;
import com.jd.dict.model.TableInfoModel;
import com.jd.dict.util.DateUtils;

import java.io.File;
import java.util.List;

public class DataDictHelper {
	public static void createTableInfoFile(TableInfoModel table) {
		String html = getTableFieldHead(table);

		FileOperator.writeFile(ConfigClass.OUT_PATH + "table_doc" + File.separator + table.getTableName() + ".html", html);
	}

	private static String getTableFieldHead(TableInfoModel table){
		StringBuffer html = new StringBuffer();
        html.append("<html>").append(System.getProperty("line.separator"));
        html.append("  <head>").append(System.getProperty("line.separator"));
        html.append("    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />").append(System.getProperty("line.separator"));
        html.append("      <title>").append(table.getTableName()).append(" ").append(table.getComment()).append("</title>").append(System.getProperty("line.separator"));
        html.append("      <style type=\"text/css\">").append(System.getProperty("line.separator"));
        html.append("      .guest_link{ background:#FCFCFC; border:solid 1px #ccc;padding:5px; margin-top:0px; margin-left:0px; margin-right:0px;margin-bottom:10px; font-size:12px;}").append(System.getProperty("line.separator"));
        html.append("        body,td,th {").append(System.getProperty("line.separator"));
        html.append("        f ont-size: 12px;").append(System.getProperty("line.separator"));
        html.append("      }").append(System.getProperty("line.separator"));
        html.append("      a:link {").append(System.getProperty("line.separator"));
        html.append("        color: #09C;").append(System.getProperty("line.separator"));
        html.append("        text-decoration: none;").append(System.getProperty("line.separator"));
        html.append("      }").append(System.getProperty("line.separator"));
        html.append("      a:visited {").append(System.getProperty("line.separator"));
        html.append("        color: #09C;").append(System.getProperty("line.separator"));
        html.append("        text-decoration: none;").append(System.getProperty("line.separator"));
        html.append("      }").append(System.getProperty("line.separator"));
        html.append("      a:hover {").append(System.getProperty("line.separator"));
        html.append("        text-decoration: none;").append(System.getProperty("line.separator"));
        html.append("      }").append(System.getProperty("line.separator"));
        html.append("      a:active {").append(System.getProperty("line.separator"));
        html.append("        text-decoration: none;").append(System.getProperty("line.separator"));
        html.append("      }").append(System.getProperty("line.separator"));
        html.append("      </style>").append(System.getProperty("line.separator"));
        html.append("  </head>").append(System.getProperty("line.separator")).append(System.getProperty("line.separator"));

        html.append("  <body leftmargin=\"3\" rightmargin=\"3\" topmargin=\"3\">").append(System.getProperty("line.separator"));
        html.append("    <div class=\"guest_link\">").append(System.getProperty("line.separator"));
        html.append("      <b><span style=\"font-size:24px\">").append(table.getComment()).append(" (").append(table.getTableName()).append(")</span></b>").append(System.getProperty("line.separator"));
        html.append("    </div>").append(System.getProperty("line.separator"));
        html.append("    <p align=\"right\" style=\"color:#CDCDCD\">&nbsp;&nbsp;  创建时间:").append(DateUtils.getCurrDate()).append(" 创建者:LIUHAIMING 数据字典生成工具 ").append("</p>").append(System.getProperty("line.separator"));

        // table title
        html.append("    字段明细:").append(System.getProperty("line.separator"));
        html.append("    <table width=\"100%\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#CCCCCC\" style=\"border-collapse: collapse;table-layout:fixed;word-wrap:break-word;\">").append(System.getProperty("line.separator"));
        html.append("      <tr bgcolor=\"#DFE9FA\" height=\"30px\">").append(System.getProperty("line.separator"));
        html.append("        <td width=\"40\" align=\"center\">序号</td>").append(System.getProperty("line.separator"));
        html.append("        <td align=\"center\">编码</td>").append(System.getProperty("line.separator"));
        html.append("        <td align=\"center\">名称</td>").append(System.getProperty("line.separator"));
        html.append("        <td align=\"center\">字段类型</td>").append(System.getProperty("line.separator"));
        html.append("        <td align=\"center\">不是NULL</td>").append(System.getProperty("line.separator"));
        html.append("        <td align=\"center\">是否主键</td>").append(System.getProperty("line.separator"));
        html.append("      </tr>").append(System.getProperty("line.separator"));
		html.append(       getTableFieldProperty(table.getFieldList()) ).append(System.getProperty("line.separator"));
		html.append("    </table>").append(System.getProperty("line.separator"));

        // table index
        html.append("    <br />").append(System.getProperty("line.separator"));
        html.append("    <br />").append(System.getProperty("line.separator"));
        html.append("    索引明细:").append(System.getProperty("line.separator"));
        html.append("    <table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#CCCCCC\" style=\"border-collapse: collapse;table-layout:fixed;word-wrap:break-word;min-width: 50%\">").append(System.getProperty("line.separator"));
        html.append("      <tr bgcolor=\"#DFE9FA\" height=\"30px\">").append(System.getProperty("line.separator"));
        html.append("        <td width=\"40\" align=\"center\">序号</td>").append(System.getProperty("line.separator"));
        html.append("        <td align=\"center\">索引键值</td>").append(System.getProperty("line.separator"));
        html.append("        <td align=\"center\">索引列</td>").append(System.getProperty("line.separator"));
        html.append("      </tr>").append(System.getProperty("line.separator"));
        html.append(       getTableIndexProperty(table.getIndexList()) ).append(System.getProperty("line.separator"));
        html.append("    </table>").append(System.getProperty("line.separator"));

        html.append("    &nbsp;\n<div align=\"right\"  class=\"guest_link\" > &copy;").append(DateUtils.getYear()).append(" 京东物流-物流研发部-青龙研发部-转运管理研发部 ").append("   &nbsp;&nbsp;&nbsp;&nbsp;</div>").append(System.getProperty("line.separator"));
		html.append("  </body>").append(System.getProperty("line.separator"));
		html.append("</html>").append(System.getProperty("line.separator"));

		return html.toString();
	}

	private static StringBuffer getTableFieldProperty(List<TableFieldInfoModel> fieldList) {
		int no = 0;
        StringBuffer property = new StringBuffer();
		for ( TableFieldInfoModel field : fieldList ) {
            no++;
			property.append(no % 2 == 1 ? "<tr>" : "<tr bgcolor=\"#F5F5F5\" >").append(System.getProperty("line.separator"));
            property.append("  <td align=\"center\">&nbsp;").append(no).append("</td>" ).append(System.getProperty("line.separator"));
            property.append("  <td>&nbsp;").append(field.getKey()).append("</td>" ).append(System.getProperty("line.separator"));
            property.append("  <td>&nbsp;").append(field.getComment()).append("</td>").append(System.getProperty("line.separator"));
            property.append("  <td align=\"center\">").append(getType(field)).append("</td>" ).append(System.getProperty("line.separator"));
            property.append("  <td align=\"center\">").append(field.isNullable() ? "" : "✓").append("</td>" ).append(System.getProperty("line.separator"));
            property.append("  <td align=\"center\">").append(field.isPrimaryKey() ? "✓" : "").append("</td>" ).append(System.getProperty("line.separator"));
            property.append("</tr>").append(System.getProperty("line.separator"));
		}

		return property;
	}

    private static StringBuffer getTableIndexProperty(List<TableIndexInfoModel> indexList) {
        int no = 0;
        StringBuffer property = new StringBuffer();
        for ( TableIndexInfoModel index : indexList ) {
            no++;
            property.append(no % 2 == 1 ? "<tr>" : "<tr bgcolor=\"#F5F5F5\" >").append(System.getProperty("line.separator"));
            property.append("  <td align=\"center\"" + ((index.getField().size() > 1) ? " rowspan=\"" + index.getField().size() + "\"" : "") + " >&nbsp;").append(no).append("</td>" ).append(System.getProperty("line.separator"));
            property.append("  <td " + ((index.getField().size() > 1) ? " rowspan=\"" + index.getField().size() + "\"" : "") + ">&nbsp;").append(index.getKey()).append("</td>" ).append(System.getProperty("line.separator"));
            property.append("  <td>&nbsp;").append(index.getField().get(0)).append("</td>").append(System.getProperty("line.separator"));
            property.append("</tr>").append(System.getProperty("line.separator"));

            if ( index.getField().size() > 1 ) {
                for (int idx = 1; idx < index.getField().size(); idx++) {
                    property.append(no % 2 == 1 ? "<tr>" : "<tr bgcolor=\"#F5F5F5\" >").append(System.getProperty("line.separator"));
                    property.append("  <td>&nbsp;").append(index.getField().get(idx)).append("</td>").append(System.getProperty("line.separator"));
                    property.append("</tr>").append(System.getProperty("line.separator"));
                }
            }
        }

        return property;
    }

	private static String getType(TableFieldInfoModel field) {
        return field.getType().toLowerCase() + "(" + field.getFieldLength() + (field.getPowerLength() > 0 ? ", " + field.getPowerLength() : "") + ")";
    }
}
